<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="handleClose" @click.native="isTreeShow = false">
    <!-- 表单 -->
    <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!-- 下拉框只能选中不能修改 - 单项绑定， v-model 是双向绑定的 -->
        <el-input
          :value="formData.departmentName"
          style="width:50%"
          placeholder="请选择部门"
          @click.native.stop="handleClick"
        />
        <!-- 树形数据 -->
        <div v-if="isTreeShow" class="tree-box">
          <el-scrollbar>
            <el-tree
              v-loading="isTreeLoading"
              :data="treeList"
              :props="defaultProps"
              @node-click="handleNodeClick"
            />
          </el-scrollbar>
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="clickSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { reqGetDepartments } from '@/api/departments'
import { transTreeData } from '@/utils'
import employeesConstant from '@/constant/employees'
import { reqAddEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      },
      treeList: [], // 树形数据
      defaultProps: {
        label: 'name'
      },
      isTreeShow: false,
      isTreeLoading: false,
      hireType: employeesConstant.hireType
    }
  },
  methods: {
    // 关闭对话框
    handleClose() {
      this.$emit('update:showDialog', false)
      //   重置表单
      this.$refs.addForm.resetFields()
    },
    // 获取部门列表
    async handleClick() {
      // 显示树形数据
      this.isTreeShow = !this.isTreeShow
      //   loading 效果
      this.isTreeLoading = true
      // 如果树形数据不显示，就不发送请求
      if (!this.isTreeShow) return
      // 发送请求获取数据
      const { data: { data: { depts }}} = await reqGetDepartments()
      // console.log(depts)
      // 将数据转换成树形数据
      const res = transTreeData(depts, '')
      //   console.log(res)
      this.treeList = res
      this.isTreeLoading = false
    },
    // 树形节点被点击时的函数
    handleNodeClick(data) {
      //   console.log(data)
      //   如果有子部门就不显示该名称
      if (data.children && data.children.length > 0) return
      //   点击之后，显示部门名称
      this.formData.departmentName = data.name
      //   隐藏树形结构
      this.isTreeShow = false
    },
    // 新增员工
    clickSubmit() {
      // 1、表单预校验
      this.$refs.addForm.validate(async(flag) => {
        if (!flag) return
        // 2、校验成功 - 发送请求
        const { data: res } = await reqAddEmployee(this.formData)
        // console.log(res)
        // 3、提示用户
        this.$message.success(res.message)
        // 4、关闭对话框
        this.handleClose()
        // 5、重新获取数据进行渲染
        this.$emit('add-employee')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-box {
  position: absolute;
  width: 50%;
//   min-height: 50px;
//   max-height: 250px;
  height: 250px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
//   overflow: auto;
  background-color: #fff;
  ::v-deep {
    .el-tree-node__content {
        height: auto;
    }
    .el-scrollbar {
        height: 100%;
    }
    .el-scrollbar__wrap {
        overflow-x: hidden;
    }
  }
}
</style>
